{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aca86665-8672-421a-b438-16270d75200d",
   "metadata": {},
   "source": [
    "The `EditableTemplate` builds on the [`VanillaTemplate`](./Vanilla.ipynb) extending it with functionality for editing the layout of the components on the page. It is a list-like variant where the `main` area acts like a list-like container, unlike the grid-like templates such as `React` and `FastGridTemplate`. Specifically it allows for the following actions:\n",
    "\n",
    "- **Hide** outputs\n",
    "- **Resize** outputs\n",
    "- **Rearrange** outputs\n",
    "\n",
    "In this way a dashboard can be built using a drag and drop approach. The resulting layout can then be persisted into local storage. To open the dashboard in `editable` mode you can either set the `editable=True` option or append `?editable=true` as a URL parameter.\n",
    "\n",
    "The `EditableTemplate` is used by the Panel layout builder functionality which makes it trivial to go from a notebook to a deployed dashboard by using the drag-and-drop interface to lay out components on the page.\n",
    "\n",
    "## Basic Templates\n",
    "\n",
    "For a large variety of use cases we do not need complete control over the exact layout of each individual component on the page, as could be achieved with a [custom template](../../explanation/styling/templates_overview.md), we just want to achieve a more polished look and feel. For these cases Panel ships with a number of default templates, which are defined by declaring four main content areas on the page, which can be populated as desired:\n",
    "\n",
    "* **`header`**: The header area of the HTML page\n",
    "* **`sidebar`**: A collapsible sidebar\n",
    "* **`main`**: The main area of the application\n",
    "* **`modal`**: A modal area which can be opened and closed from Python\n",
    "\n",
    "These four areas behave very similarly to other Panel layout components and have list-like semantics. This means we can easily append new components into these areas. Unlike other layout components however, the contents of the areas is fixed once rendered. If you need a dynamic layout you should therefore insert a regular Panel layout component (e.g. a `Column` or `Row`) and modify it in place once added to one of the content areas. \n",
    "\n",
    "Templates can allow for us to quickly and easily create web apps for displaying our data. Panel comes with a default Template, and includes multiple Templates that extend the default which add some customization for a better display.\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "In addition to the four different areas we can populate the default templates also provide a few additional parameters:\n",
    "\n",
    "* **`busy_indicator`** (BooleanIndicator): Visual indicator of application busy state.\n",
    "* **`collapsed_sidebar`** (str, `default=False`): Whether the sidebar (if present) is initially collapsed.\n",
    "* **`editable`** (bool, `default=True`): Whether the layout of the template should be editable.\n",
    "* **`header_background`** (str): Optional header background color override.\n",
    "* **`header_color`** (str): Optional header text color override.\n",
    "* **`logo`** (str): URI of logo to add to the header (if local file, logo is base64 encoded as URI).\n",
    "* **`site`** (str): Name of the site. Will be shown in the header. Default is '', i.e. not shown.\n",
    "* **`site_url`** (str): Url of the site and logo. Default is \"/\".\n",
    "* **`title`** (str): A title to show in the header.\n",
    "* **`theme`** (Theme): A Theme class (available in `panel.template.theme`)\n",
    "* **`sidebar_width`** (int): The width of the sidebar in pixels. Default is 330.\n",
    "\n",
    "________\n",
    "\n",
    "The `EditableTemplate` can be populated like any other list-like template, by extending the `main` layout:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00c8054f-e160-42bd-a86d-0d1d13073f11",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas\n",
    "import numpy as np\n",
    "import panel as pn\n",
    "import pandas as pd\n",
    "\n",
    "xs = np.linspace(0, np.pi)\n",
    "\n",
    "freq = pn.widgets.FloatSlider(name=\"Frequency\", start=0, end=10, value=2)\n",
    "phase = pn.widgets.FloatSlider(name=\"Phase\", start=0, end=np.pi)\n",
    "\n",
    "def sine(freq, phase):\n",
    "    return pd.DataFrame(dict(y=np.sin(xs*freq+phase)), index=xs)\n",
    "\n",
    "def cosine(freq, phase):\n",
    "    return pd.DataFrame(dict(y=np.cos(xs*freq+phase)), index=xs)\n",
    "\n",
    "dfi_sine = hvplot.bind(sine, freq, phase).interactive()\n",
    "dfi_cosine = hvplot.bind(cosine, freq, phase).interactive()\n",
    "\n",
    "plot_opts = dict(responsive=True, min_height=400)\n",
    "\n",
    "# Instantiate the template with widgets displayed in the sidebar\n",
    "template = pn.template.EditableTemplate(\n",
    "    editable=True,\n",
    "    title='EditableTemplate',\n",
    "    sidebar=[freq, phase],\n",
    ")\n",
    "# Append a layout to the main area, to demonstrate the list-like API\n",
    "template.main.extend([\n",
    "    dfi_sine.hvplot(title='Sine Plot', **plot_opts).output(),\n",
    "    dfi_cosine.hvplot(title='Cosine Plot', **plot_opts).output(),\n",
    "])\n",
    "\n",
    "template.servable();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "992ec479-c8a6-4555-a2c4-8d2eee1563f3",
   "metadata": {},
   "source": [
    "The resulting default layout will simply initialize each component on top of one another:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8bfb9b3-a446-4b34-a2aa-9208af6d6d47",
   "metadata": {},
   "source": [
    "<img src=\"../../assets/EditableTemplate.png\" style=\"margin-left: auto; margin-right: auto; display: block;\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecd35ffd-180f-4685-878f-a04f76bb645e",
   "metadata": {},
   "source": [
    "If the template is in editable mode we can delete, resize, and rearrange the outputs on the page using a drag and drop interface. \n",
    "\n",
    "- Delete: Hover on the top-right to reveal the delete icon\n",
    "\n",
    "<img src=\"\"></img>\n",
    "\n",
    "- Resize: Hover on the bottom-right to reveal the resize handle\n",
    "\n",
    "<img src=\"\"></img>\n",
    "\n",
    "\n",
    "- Rearrange: Hover on the top-left to reveal the drag icon\n",
    "\n",
    "<img src=\"\"></img>\n",
    "\n",
    "In edit mode we additionally have undo, reset and save icons in the header:\n",
    "\n",
    "- Undo: Undo previous delete, resize and re-arrange actions.\n",
    "\n",
    "<img src=\"\" width=\"24\" height=\"24\"></img>\n",
    "\n",
    "- Reset: Resets the arrangement and order of the individual components to the default.\n",
    "\n",
    "<img src=\"\" width=\"24\" height=\"24\"></img>\n",
    "\n",
    "- Save: Persists the current layout into the browser storage overriding the layout persisted on the server.\n",
    "\n",
    "<img src=\"\"></img>\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
